package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import org.springframework.security.access.prepost.PreAuthorize;
import java.util.*;

/**
 * user :   狐狸 🦊
 * date : 2020/11/21
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private UserService userService;
    @Reference
    private MenuService menuService;
    /**
     * 获取菜单
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenu() {

        log.info("[获取菜单]:~~~~");
        //获取用户
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        com.itheima.health.pojo.User userPojo = userService.findByUsername(username);
        //获取用户角色
        Set<Role> roles = userPojo.getRoles();
        //需要返回给前端的数据
        List<Map<String, Object>> menu = new ArrayList<>();
        for (Role role : roles) {
            List<Menu> menus = menuService.findByRoleId(role.getId());
            for (Menu menu1 : menus) {
                Map<String, Object> map = new HashMap<>();
                map.put("path", menu1.getPath());
                map.put("title", menu1.getName());
                map.put("icon", menu1.getIcon());
                map.put("children", new ArrayList<>());
                menu.add(map);

                List<Menu> children = menu1.getChildren();
                for (Menu child : children) {
                    Map<String, Object> map1 = new HashMap<>();
                    map1.put("path", child.getPath());
                    map1.put("title", child.getName());
                    map1.put("linkUrl", child.getLinkUrl());
                    ((List<Map<String, Object>>) map.get("children")).add(map1);
                }
            }
        }
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menu);
    }

            @PostMapping("/add")
            @PreAuthorize("hasAuthority('MENU_ADD')")
            public Result add (@RequestBody Menu menu){
                log.info("[菜单-新增]data:{}", menu);
                menuService.add(menu);
                return new Result(true, "新增菜单成功");
            }


            @GetMapping("findPage")
            @PreAuthorize("hasAuthority('MENU_QUERY')")
            public Result findPage (QueryPageBean queryPageBean){
                log.info("[菜单-分页查询]data:{}", queryPageBean);
                //rpc查询数据
                PageResult pageResult = menuService.pageQuery(queryPageBean);
                //封装返回结果
                return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
            }

            @RequestMapping("/delete")
            @PreAuthorize("hasAuthority('MENU_DELETE')")
            public Result delete (Integer id){
                log.info("[菜单-根据id删除]id：{}", id);
                //RPC调用处理业务
                menuService.delete(id);
                return new Result(true, "删除菜单成功");
            }

            @RequestMapping("/edit")
            @PreAuthorize("hasAuthority('MENU_EDIT')")
            public Result edit (@RequestBody Menu menu){
                log.info("[菜单-编辑]data:", menu);
                menuService.edit(menu);
                return new Result(true, "编辑菜单成功");
            }

            @RequestMapping("/findById")
            @PreAuthorize("hasAuthority('MENU_QUERY')")
            public Result findById (Integer id){
                log.info("[菜单-根据ID查询]id:{}", id);
                Menu menu = menuService.findById(id);
                return new Result(true, MessageConst.ACTION_SUCCESS, menu);
            }

            @RequestMapping("/findAll")
            @PreAuthorize("hasAuthority('MENU_QUERY')")
            public Result findAll () {
                log.info("[菜单-查询所有]~");
                List<Menu> menus = menuService.findAll();
                return new Result(true, "查询菜单成功", menus);
            }

            @RequestMapping("/findmenuIdsByRoleid")
            public Result findmenuIdsByRoleid(Integer id) {
                List<Integer> integers = menuService.findmenuIdsByRoleid(id);
                return new Result(true, "获取菜单id成功", integers);
            }

}